from rest_framework.viewsets import ModelViewSet
from meiduo_admin.my_paginate import MyPageNumberPagination
from goods.models import SpecificationOption,SPUSpecification
from . import spec_options_serializers
from rest_framework.generics import ListAPIView

#1,spec options
class SpecOptionsViewSet(ModelViewSet):
    pagination_class = MyPageNumberPagination
    queryset = SpecificationOption.objects.all()
    serializer_class = spec_options_serializers.SpecOptionsSerializer

#2,spec 简要信息
class SpecListView(ListAPIView):
    serializer_class = spec_options_serializers.SpecListSerializer
    # queryset = SPUSpecification.objects.all()

    #1,重写方法,将spu.name拼接到name中
    def get_queryset(self):
        #1,获取所有的规格
        queryset = SPUSpecification.objects.all()

        #2,拼接spu.name
        for spec in queryset:
            spec.name = "%s-%s"%(spec.spu.name,spec.name)

        #3,返回数据
        return queryset


